package com.example.result.books

import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.window.Dialog
import androidx.datastore.core.DataStore
import com.lly.tboc.zhouyi.Screen.FileType
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Composable
fun SelectFileTypeDialog(
    suffix: String,
    fileTypeMapDataStore: DataStore<FileTypeMap>,
    selectOptionDialog: MutableState<Boolean>
) {
    val scope = rememberCoroutineScope()
    val action = { type: FileType ->
        scope.launch(Dispatchers.Default) {
            fileTypeMapDataStore.updateData {
                it.copy(map = it.map.toMutableMap().apply {
                    when (type) {
                        FileType.UnKnow -> remove(type.name)
                        else -> put(suffix, type.name)
                    }
                })
            }
        }
        selectOptionDialog.value = false
    }
    if (selectOptionDialog.value) {
        Dialog(onDismissRequest = { selectOptionDialog.value = false }) {
            Card {
                Button(onClick = {
                    action(FileType.Audio)
                }) {
                    Text(text = "音/视频")
                }
                Button(onClick = {
                    action(FileType.Txt)

                }) {
                    Text(text = "文本")
                }
                Button(onClick = {
                    action(FileType.Markdown)

                }) {
                    Text(text = "Markdown")
                }
                Button(onClick = {
                    action(FileType.Bitmap)
                }) {
                    Text(text = "图片")
                }
                Button(onClick = {
                    action(FileType.Svg)
                }) {
                    Text(text = "Svg")
                }
                Button(onClick = {
                    selectOptionDialog.value = false
                    action(FileType.UnKnow)
                }) {
                    Text(text = "暂不支持")
                }
            }
        }
    }
}
